package com.ymatou.apigateway.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.CatalogInfo;
import com.ymatou.apigateway.parameter.GetproductDetailBean;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.ProductInfo;
import com.ymatou.apigateway.parameter.PropertyInfo;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.ShenqiGetProductDetailCall;
import com.shop2cn.iapi.agentproducts.parameter.OpenProductDto;
import com.shop2cn.iapi.agentproducts.parameter.QueryProductListBean;
import com.shop2cn.iapi.agentproducts.service.QueryProductListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
*获取单个商品详细信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_sqGetProductDetail {

	private static OpenapiReq openapiReq;
	private static GetproductDetailBean getproductdetailBean;
	private static ShenqiGetProductDetailCall shenqiGetProductDetailCall;
	
	private static QueryProductListBean queryproductlistBean = new QueryProductListBean();
	private static QueryProductListCall queryproductlistCall = new QueryProductListCall();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	private static long multiCDFCodeGoodsOwnerId = ShenqiUserSet.multiCDFCodeGoodsOwnerId;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取神器单个商品详细信息");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.products.detail.get");

		getproductdetailBean = new GetproductDetailBean();
		shenqiGetProductDetailCall = new ShenqiGetProductDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shenqiGetProductDetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify(String productId, int userId, ProductInfo info) throws Exception {
		
		queryproductlistBean.setProductIdList(Arrays.asList(productId));
		queryproductlistBean.setUserId(userId);
		queryproductlistBean.setPageNo(1);
		queryproductlistBean.setPageSize(10);
		
		queryproductlistCall.setData(queryproductlistBean);
		queryproductlistCall.callService();
		
		OpenProductDto expectProduct = queryproductlistCall.getProductList().get(0);
		
		DataUtil.verifyEquals(expectProduct.getThirdCategoryName(), info.getCategory_name(), "验证category_name");
		DataUtil.verifyEquals(null, info.getProduct_url(), "验证product_url");
		DataUtil.verifyEquals(expectProduct.getCreateTime(), info.getCreate_time(), "验证create_time");
		DataUtil.verifyEquals(expectProduct.getParentMchName(), info.getVendor_name(), "验证vendor_name");
		DataUtil.verifyEquals(expectProduct.getBrandName(), info.getBrand_name(), "验证brand_name");
		DataUtil.verifyEquals(expectProduct.getRemark(), info.getRemark(), "验证remark");
		DataUtil.verifyEquals(expectProduct.getProductName(), info.getProduct_name(), "验证product_name");
		DataUtil.verifyEquals(expectProduct.getBrandId(), info.getBrand_id(), "验证brand_id");
		DataUtil.verifyEquals(expectProduct.getUpdateTime(), info.getUpdate_time(), "验证update_time");
		DataUtil.verifyEquals(expectProduct.getThirdCategoryId(), info.getCategory_id(), "验证category_id");
		DataUtil.verifyEquals(expectProduct.getDeliverType(), info.getDelivery_type(), "验证delivery_type");
		DataUtil.verifyEquals(expectProduct.getProductId(), info.getProduct_id(), "验证product_id");
		DataUtil.verifyEquals(expectProduct.getParentMchId(), info.getVendor_id(), "验证vendor_id");
		
		for (int i = 0; i < info.getSkus().size(); i++) {
			
			CatalogInfo catalogInfo = info.getSkus().get(i);
			
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.getExtraInfo(), catalogInfo.getExtra_info(),
					"验证extra_info");
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.getWeightUnit(), catalogInfo.getWeight_unit(),
					"验证weight_unit");
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.getSaleCurrency(), catalogInfo.getCurrency_type(),
					"验证currency_type");
			DataUtil.verifyDecimalEquals(expectProduct.getSkuList().get(i)
					.getSalePrice(), catalogInfo.getPrice(),
					"验证price");
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.getWeight(), catalogInfo.getWeight(),
					"验证weight");
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.isEnable(), catalogInfo.getUsed(),
					"验证used");
			DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
					.getBuyingCurrency(), catalogInfo.getVendor_currency(),
					"验证vendor_currency");
			DataUtil.verifyDecimalEquals(expectProduct.getSkuList().get(i)
					.getBuyingPrice(), catalogInfo.getVendor_price(),
					"验证vendor_price");
			
			for (int j = 0; j < catalogInfo.getProperties().size(); j++) {
				
				PropertyInfo propertyInfo = catalogInfo.getProperties().get(j);
				
				DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
						.getCatalogPropertyList().get(j).getPropertyName(),
						propertyInfo.getName(), "验证name");
				DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
						.getCatalogPropertyList().get(j).getPropertyUrl(),
						propertyInfo.getPic_url(), "验证pic_url");
				DataUtil.verifyEquals(expectProduct.getSkuList().get(i)
						.getCatalogPropertyList().get(j).getPropertyValue(),
						propertyInfo.getValue(), "验证value");
			}
		}
		
		for (int i = 0; i < expectProduct.getPicList().size(); i++)
			DataUtil.verifyEquals(expectProduct.getPicList().get(i),
					info.getProduct_images()[i], "验证product_images");

	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqGetProductDetail_001() {
		Logger.start(true, "店主获取单个商品详细信息");
		try {
			
//			String productID = DGSQBuildProxyChainCallService
//					.createMultiCodeProxychain((int) multiCDFCodeGoodsOwnerId,
//							false).getProductId();
			
			String productID = "p5595303";
			long sellerId = 502381949L;

			getproductdetailBean.setProduct_id(productID);

			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");

			ProductInfo info1 = shenqiGetProductDetailCall.getProductInfo();
			
			this.verify(productID, (int)multiCDFCodeGoodsOwnerId, info1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqGetProductDetail_002() {
		Logger.start(true, "中间商获取单个商品详细信息");
		try {
			
			String productID = "p1466629";
			long sellerId = 500004880L;

			getproductdetailBean.setProduct_id(productID);

			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");

			ProductInfo info1 = shenqiGetProductDetailCall.getProductInfo();
			
			this.verify(productID, (int)sellerId, info1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqGetProductDetail_003() {
		Logger.start(true, "货头获取单个商品详细信息");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			String productID = productBean.getRootProductId();

			getproductdetailBean.setProduct_id(productID);

			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(autotest_goodsOwnerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");

			ProductInfo info1 = shenqiGetProductDetailCall.getProductInfo();
			
			this.verify(productID, (int)autotest_goodsOwnerId, info1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqGetProductDetail_004() {
		Logger.start(true, "商品Idnull");
		try {
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(this.autotest_goodsOwnerId)
					.get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0001", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");
			Logger.verifyEquals("productId:may not be empty",
					shenqiGetProductDetailCall.getMessage(), "验证返回message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqGetProductDetail_005() {
		Logger.start(true, "商品Id不存在");
		try {
			
			String productID = "883a4206-957f-409a-86e3-1bade555";
			getproductdetailBean.setProduct_id(productID);

			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(this.autotest_goodsOwnerId)
					.get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");
			Logger.verifyEquals(null, shenqiGetProductDetailCall.getProductInfo(),
					"验证返回ProductInfo");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_004() {
		Logger.start(true, "商品Id与Seller不匹配");
		try {

			int sellerId = 288484;
			String productID = "883a4206-957f-409a-86e3-6aa734d1bade";
			Thread.sleep(1000);

			getproductdetailBean.setProduct_id(productID);

			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(sellerId)).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");
			Logger.verifyEquals(null, shenqiGetProductDetailCall.getProductInfo(),
					"验证返回ProductInfo");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetproductDetail_005() {
		Logger.start(true, "获取单个商品详细信息-stg");
		try {
			
			String productID1 = "e123d00a-6668-47ea-a583-ff1f2642a3e1";
			getproductdetailBean.setProduct_id(productID1);

			String app_id = "i2UyscTxmxDoyfWnIz";
			String app_secret = "95605832511B6C85DB2D0FA9E3ACBB61FE3E1730322EF9CCA09E7ED5E523721461BEC16080D01798E84218559F4A6177";
			String auth_code = "ADMZyKjNhHI5dYJepFuMK3pdAwIUNj4D";

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(auth_code);
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));

			String url = String
					.format("https://open.cdfmembers.com/apigateway/v1?app_id=%s&method=sq.products.detail.get",
							app_id);
			shenqiGetProductDetailCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			shenqiGetProductDetailCall.setData(openapiReq);
			shenqiGetProductDetailCall.callService();

			Logger.verifyEquals("0000", shenqiGetProductDetailCall.getCode(),
					"验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}